জাভার Streams API এবং Generics একসাথে ব্যবহার করে ডেটা প্রসেসিং আরও শক্তিশালী, ফ্লেক্সিবল এবং টাইপ-সেইফ করা যায়। Generics Streams API-তে টাইপের নিরাপত্তা নিশ্চিত করে এবং ডেটা প্রসেসিংয়ে কম্পাইল-টাইম চেকিং নিশ্চিত করে।
Generics এবং Streams API এর সম্পর্ক
- Generics Collections: Streams API সাধারণত জেনেরিক কালেকশন (যেমন
List<T>,Set<T>) থেকে স্ট্রিম তৈরি করতে ব্যবহৃত হয়। - Type-Safe Operations: Generics Streams API-তে টাইপ-সেইফ অপারেশন নিশ্চিত করে, ফলে টাইপ-কাস্টিং এর প্রয়োজন হয় না।
- Reusable Pipelines: জেনেরিক্সের সাহায্যে Streams API-তে ডেটা প্রসেসিং পাইপলাইন রিইউজেবল করা যায়।
Streams API এবং Generics এর ব্যবহার: উদাহরণ
1. স্ট্রিম তৈরি এবং জেনেরিক প্রসেসিং
import java.util.Arrays;
import java.util.List;
public class GenericStreamExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
// Stream তৈরি এবং প্রসেসিং
names.stream()
.filter(name -> name.startsWith("A"))
.forEach(System.out::println); // Output: Alice
}
}
2. জেনেরিক মেথড ব্যবহার করে স্ট্রিম প্রসেসিং
import java.util.List;
import java.util.stream.Collectors;
public class GenericStreamMethod {
// জেনেরিক মেথড
public static <T> List<T> filterItems(List<T> items, java.util.function.Predicate<T> predicate) {
return items.stream()
.filter(predicate)
.collect(Collectors.toList());
}
public static void main(String[] args) {
List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6);
// Even সংখ্যা ফিল্টার করা
List<Integer> evenNumbers = filterItems(numbers, num -> num % 2 == 0);
System.out.println("Even Numbers: " + evenNumbers); // Output: [2, 4, 6]
List<String> words = List.of("apple", "banana", "cherry", "date");
List<String> shortWords = filterItems(words, word -> word.length() <= 5);
System.out.println("Short Words: " + shortWords); // Output: [apple, banana, cherry, date]
}
}
3. Map এবং Reduce অপারেশন (Generics সহ)
import java.util.Arrays;
import java.util.List;
public class GenericStreamMapReduce {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Map এবং Reduce অপারেশন
int sumOfSquares = numbers.stream()
.map(num -> num * num) // প্রতিটি সংখ্যার স্কোয়ার
.reduce(0, Integer::sum); // সবগুলোর যোগফল
System.out.println("Sum of Squares: " + sumOfSquares); // Output: 55
}
}
4. Custom Class এবং Streams API
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
class Product {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
@Override
public String toString() {
return name + ": $" + price;
}
}
public class GenericStreamWithCustomClass {
public static void main(String[] args) {
List<Product> products = Arrays.asList(
new Product("Laptop", 800.00),
new Product("Phone", 500.00),
new Product("Tablet", 300.00),
new Product("Headphones", 50.00)
);
// ফিল্টার করে প্রোডাক্টের নামের তালিকা তৈরি
List<String> expensiveProductNames = products.stream()
.filter(product -> product.getPrice() > 300.00)
.map(Product::getName)
.collect(Collectors.toList());
System.out.println("Expensive Products: " + expensiveProductNames);
// Output: [Laptop, Phone]
}
}
5. Generics এবং Parallel Streams
import java.util.Arrays;
import java.util.List;
public class ParallelStreamExample {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
// Parallel Stream ব্যবহার
int sum = numbers.parallelStream()
.filter(num -> num % 2 == 0) // Even সংখ্যা ফিল্টার করা
.mapToInt(num -> num * 2) // প্রতিটি সংখ্যা দ্বিগুণ করা
.sum();
System.out.println("Sum of doubled even numbers: " + sum); // Output: 60
}
}
Generics এবং Streams API এর সুবিধা
- Reusable Methods: একবার জেনেরিক মেথড বা ক্লাস লিখে তা বিভিন্ন টাইপের জন্য পুনর্ব্যবহার করা যায়।
- Type Safety: টাইপ সম্পর্কিত ভুল এড়ানো যায় কারণ কম্পাইল-টাইম চেকিং নিশ্চিত হয়।
- Concise Code: Generics এবং Streams API ব্যবহার করলে কোড আরও সংক্ষিপ্ত ও সহজপাঠ্য হয়।
- Parallel Processing: Generics ব্যবহার করেও সহজে প্যারালাল প্রসেসিং সম্ভব।
Generics এবং Streams API একত্রে ব্যবহার করলে জাভার ডেটা প্রসেসিং আরও কার্যকর ও টাইপ-সেইফ হয়। Collections Framework থেকে সহজে স্ট্রিম তৈরি, জেনেরিক মেথডের মাধ্যমে ফ্লেক্সিবল পাইপলাইন তৈরি, এবং জটিল ডেটা প্রক্রিয়া সহজতর করার জন্য Generics অত্যন্ত কার্যকর। সঠিকভাবে Generics এবং Streams API ব্যবহার করলে প্রোগ্রামিং আরও কার্যকর ও রিইউজেবল হয়ে ওঠে।
Content added By
Read more